<?php

/**
 * WaterMsgtpl校园订水消息发送模版
 */
class WaterMsgtpl extends BaseMsgtpl
{

    /**
     * 给用户发消息
     */
    public function sendMessageToUser($orderInfo, $accountId)
    {
        $condition = array('user_id' => $orderInfo['user_id'],);

        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition, 'openid,subscribe');
        $url = C('site_url') . U('base/water/orderList', array('water_id' => $orderInfo['water_id']));
        if ($wechatUser['subscribe']) {
            if ($accountId == 2) { //滨江后勤
                $template_id = '-wKq2Sg22MBCCCsT_uVwOSOfMm1l0aRtml1vMlWLB88';
            } elseif ($accountId == 5) { //农林
                $template_id = 'ATOvt4E9ucFQACkpS8_l7GTlZVGlVt-PiaEZD7u0-Po';
            }/*elseif($accountId == 28){//云感
                $template_id = '-o2OC55fvrdUsNuXd0wFsM2hO3Rn8ey277dCpJTg3JI';
            }*/ elseif ($accountId == 25) { // 杭电
                $template_id = 'LBE9PfqXdEyLeZhCisbLfiSulCRxxu1vkgV1Rq1RNK0';
            }
            $snack_name = '';
            if ($orderInfo['snack_arr']) {
                foreach ($orderInfo['snack_arr'] as $k => $v) {
                    $snack_info = $v['snack_name'] . '*' . $v['snack_num'];
                    $snack_name = $snack_name . '' . $snack_info;
                }
            }
            $order_info = $orderInfo['goods_name'] . '*' . $orderInfo['goods_num'];
            $orderInfo['goods_name'] = $order_info . '' . $snack_name;
            $info = array(
                'first' => array("value" => '你已成功购买' . $orderInfo['goods_name'] . '。由于目前寝室调整，导致送水、送机量暴增，您订的水可能会出现延误，我们将尽快送达，造成不便敬请谅解'),
                'keyword1' => array("value" => $orderInfo['username']),
                'keyword2' => array("value" => $orderInfo['order_amount']),
                'keyword3' => array("value" => date('Y-m-d H:i')),
                'remark' => array("value" => '详情请登录系统查看'),
            );
            $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $template_id, $info, $url);
        }
    }


    /**
     * 给管理员发消息
     */
    public function sendMessageToManager($orderInfo, $accountId)
    {
        $mWaterManager = D('Base/WaterManager');
        if ($orderInfo['community_name']) {
            //查询后台设置的管理员信息
            $waterManager = $mWaterManager->getInfo(array('address' => $orderInfo['community_name'], 'account_id' => $accountId, 'cut_off' => 0));
            if (!$waterManager) {
                $waterManager = $mWaterManager->getInfo(array('address' => '默认', 'account_id' => $accountId, 'cut_off' => 0));
            }
        } else {
            $waterManager = $mWaterManager->getInfo(array('address' => '默认', 'account_id' => $accountId, 'cut_off' => 0));
        }
        if ($waterManager) {
            $condition = array('wechat_user_id' => $waterManager['wechat_user_id']);
            //查询微信用户信息
            $wechatUser = D('Wechat/WechatUser')->getInfo($condition, 'openid,subscribe');
            if ($wechatUser['subscribe']) {
                if ($accountId == 5) {  //农林
                    $templateId = 'ATOvt4E9ucFQACkpS8_l7GTlZVGlVt-PiaEZD7u0-Po';
                } elseif ($accountId == 25) { // 杭电
                    $templateId = 'LBE9PfqXdEyLeZhCisbLfiSulCRxxu1vkgV1Rq1RNK0';
                } elseif ($accountId == 28) {
                    $templateId = 'kThRqjVVDJuM2YAPIcaMJk016EAKXuQun-b4cjbhKVE';
                    //$templateId = '9gbBd7UD_FDzmz3FItseRsM-FCvfDIvGaYoYrQ-vBmA';
                } elseif ($accountId == 60) {
                    $templateId = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs';
                }
                //消息里添加零食
                $snack_name = '';
                if ($orderInfo['snack_arr']) {
                    foreach ($orderInfo['snack_arr'] as $k => $v) {
                        $snack_info = $v['snack_name'] . '*' . $v['snack_num'];
                        $snack_name = $snack_name . '' . $snack_info;
                    }
                    $order_info = $orderInfo['goods_name'] . '*' . $orderInfo['goods_num'];
                    $orderInfo['goods_name'] = $order_info . '' . $snack_name;
                    if ($accountId == 28) {
                        $info = array(
                            'first' => array("value" => "你有一条新订水订单！" . "\n派送地址：{$orderInfo['address']}" . "\n联系人：{$orderInfo['username']}" . "\n电话：{$orderInfo['mobile']}" . "\n 详情：{$orderInfo['goods_name']}"),
                            'keyword1' => array("value" => '新订水订单来了'),
                            'keyword2' => array("value" => date('Y-m-d H:i')),
                            'remark' => array("value" => '详情请登录系统查看'),
                        );
                    } else {
                        $info = array(
                            'first' => array("value" => $orderInfo['address'] . '已成功购买' . $orderInfo['goods_name'] . '，请登陆后台查看', 'color' => '#173177'),
                            'keyword1' => array("value" => $orderInfo['username']),
                            'keyword2' => array("value" => $orderInfo['order_amount'], "color" => "#173177"),
                            'keyword3' => array("value" => date('Y-m-d H:i'), "color" => "#173177"),
                            'remark' => array("value" => '详情请登录系统查看'),
                        );
                    }
                } else {
                    if ($accountId == 28) {
                        $info = array(
                            'first' => array("value" => "你有一条新订水订单！" . "\n派送地址：{$orderInfo['address']}" . "\n联系人：{$orderInfo['username']}" . "\n电话：{$orderInfo['mobile']}" . "\n 数量：{$orderInfo['goods_num']}桶"),
                            'keyword1' => array("value" => '新订水订单来了'),
                            'keyword2' => array("value" => date('Y-m-d H:i')),
                            'remark' => array("value" => '详情请登录系统查看'),
                        );
                    } else {
                        $info = array(
                            'first' => array("value" => $orderInfo['address'] . '已成功购买' . $orderInfo['goods_name'] . '，请登陆后台查看', 'color' => '#173177'),
                            'keyword1' => array("value" => $orderInfo['username']),
                            'keyword2' => array("value" => $orderInfo['order_amount'], "color" => "#173177"),
                            'keyword3' => array("value" => date('Y-m-d H:i'), "color" => "#173177"),
                            'remark' => array("value" => '详情请登录系统查看'),
                        );
                    }
                }
                $url = $accountId == 28 || $accountId == 60 ? C('site_url') . U('base/water/sendList', array('water_id' => $orderInfo['water_id'])) : '';
                $result = $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $templateId, $info, $url);
                $data = array(
                    'water_id' => $orderInfo['water_id'],
                    'manager_id' => $waterManager['manager_id'],
                    'wechat_user_id' => $waterManager['wechat_user_id'],
                    'order_id' => $orderInfo['order_id'],
                    'status' => 0,
                    'create_time' => time()
                );
                if ($result && $result['status'] == 'S') {
                    $data['status'] = 1;
                }
                M('WaterSendlog')->update($data);

            }

        }

    }


    /**
     * 下单完成给所在区域的送水工推送消息
     */
    public function sendMessageToWater($orderInfo, $accountId)
    {

        $mWaterSubarea = M('WaterSubarea');
        $building_id = $orderInfo['building_id'];
        $community_id=$orderInfo['community_id'];
        if ($community_id && $building_id) {
            //查询后台设置的管理员信息
            $condition['cut_off'] = 0;
            $condition['_string'] = "FIND_IN_SET("."$building_id".",building_id)  AND FIND_IN_SET("."$community_id".",community_id)";;

            $supplier_id = $mWaterSubarea->where($condition)->getField('supplier_id');

            $supplierInfo = M('WaterSupplier')->where(array('supplier_id' => $supplier_id))->find();

            $userInfo = M('User')->where(array('real_name' => $supplierInfo['supplier_name'], 'mobile' => $supplierInfo['mobile'], 'account_id' => $accountId))->find();

            $wechatInfo = M('WechatUser')->where(array('user_id' => $userInfo['user_id'], 'subscribe' => 1))->find();

            if ($wechatInfo) {
                $map = array('wechat_user_id' => $wechatInfo['wechat_user_id']);
                //查询微信用户信息
                $wechatUser = D('Wechat/WechatUser')->getInfo($map, 'openid,subscribe');
                if ($wechatUser['subscribe']) {
                    if ($accountId == 5) {  //农林
                        $templateId = 'ATOvt4E9ucFQACkpS8_l7GTlZVGlVt-PiaEZD7u0-Po';
                    } elseif ($accountId == 25) { // 杭电
                        $templateId = 'LBE9PfqXdEyLeZhCisbLfiSulCRxxu1vkgV1Rq1RNK0';
                    } elseif ($accountId == 28) {
                        $templateId = 'kThRqjVVDJuM2YAPIcaMJk016EAKXuQun-b4cjbhKVE';
                        //$templateId = '9gbBd7UD_FDzmz3FItseRsM-FCvfDIvGaYoYrQ-vBmA';
                    } elseif ($accountId == 60) {
                        $templateId = 'Rf7JatKFZNlX-Fji4e2AmRwCvffF8bCsUxdnsHCHAZs';
                    }

                    if ($accountId == 28) {
                        $info = array(
                            'first' => array("value" => "你有一条新订水订单！" . "\n派送地址：{$orderInfo['address']}" . "\n联系人：{$orderInfo['username']}" . "\n电话：{$orderInfo['mobile']}" . "\n 数量：{$orderInfo['goods_num']}桶"),
                            'keyword1' => array("value" => '新订水订单来了'),
                            'keyword2' => array("value" => date('Y-m-d H:i')),
                            'remark' => array("value" => '详情请登录系统查看'),
                        );
                    } else {
                        $info = array(
                            'first' => array("value" => $orderInfo['address'] . '已成功购买' . $orderInfo['goods_name'] . '，请登陆后台查看', 'color' => '#173177'),
                            'keyword1' => array("value" => $orderInfo['username']),
                            'keyword2' => array("value" => $orderInfo['order_amount'], "color" => "#173177"),
                            'keyword3' => array("value" => date('Y-m-d H:i'), "color" => "#173177"),
                            'remark' => array("value" => '详情请登录系统查看'),
                        );
                    }

                    $url = $accountId == 28 || $accountId == 60 ? C('site_url') . U('base/water/sendList', array('water_id' => $orderInfo['water_id'])) : '';
                    $result = $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $templateId, $info, $url);
                    $data = array(
                        'water_id' => $orderInfo['water_id'],
                        'wechat_user_id' => $wechatInfo['wechat_user_id'],
                        'order_id' => $orderInfo['order_id'],
                        'status' => 0,
                        'create_time' => time()
                    );
                    if ($result && $result['status'] == 'S') {
                        $data['status'] = 1;
                    }
                    M('WaterSendlog')->update($data);

                }

            }

        }
    }

    /**
     * 订水完成发消息
     */
    public function sendoverMessageToUser($orderInfo, $accountId)
    {

        $condition = array('user_id' => $orderInfo['user_id']);
        //查询微信用户信息
        $wechatUser = D('Wechat/WechatUser')->getInfo($condition, 'openid,subscribe');
        if ($wechatUser['subscribe']) {
            if ($accountId == 5) {  //农林
                $templateId = 'ATOvt4E9ucFQACkpS8_l7GTlZVGlVt-PiaEZD7u0-Po';
            } elseif ($accountId == 25) { // 杭电
                $templateId = 'LBE9PfqXdEyLeZhCisbLfiSulCRxxu1vkgV1Rq1RNK0';
            } elseif ($accountId == 28) {
                $templateId = 'kThRqjVVDJuM2YAPIcaMJk016EAKXuQun-b4cjbhKVE';
                //$templateId = '9gbBd7UD_FDzmz3FItseRsM-FCvfDIvGaYoYrQ-vBmA';
            }

            if ($accountId == 28) {
                $info = array(
                    'first' => array("value" => "你的订水已送达"),
                    'keyword1' => array("value" => '订水'),
                    'keyword2' => array("value" => date('Y-m-d H:i')),
                    'remark' => array("value" => '详情请登录系统查看'),
                );
            } else {
                /*$info = array(
                    'first' => array("value" => $orderInfo['address'].'已成功购买'.$orderInfo['goods_name'].'，请登陆后台查看', 'color' => '#173177'),
                    'keyword1' => array("value" => $orderInfo['username']),
                    'keyword2' => array("value" => $orderInfo['order_amount'], "color" => "#173177"),
                    'keyword3' => array("value" => date('Y-m-d H:i'), "color" => "#173177"),
                    'remark' => array("value" => '详情请登录系统查看'),
                );*/
            }
            $url = '';
            $result = $this->wxObj->messageTpl($this->wechatInfo['appid'], $this->wechatInfo['appsecret'], $wechatUser['openid'], $templateId, $info, $url);


        }


    }

}
